package com.fanqwz.kotlinmvvm.ui.compositor

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * Created by fanqwz on 2017/11/2.
 */
abstract class ViewCompositor {
    private val children = mutableListOf<ViewCompositor>()

    abstract fun inflateView(layoutInflater: LayoutInflater): View?

    // 当前viewCompositor添加child的逻辑
    open fun addView(root: ViewGroup, child: View?, compositor: ViewCompositor) {
        root.addView(child)
    }

    fun addChild(view: ViewCompositor) {
        children.add(view)
    }

    fun buildViewLayout(layoutInflater: LayoutInflater): View? {
        return inflateView(layoutInflater)?.let {
            if (it is ViewGroup)
                for (compositor in children) {
                    compositor.buildViewLayout(layoutInflater)?.let { child ->
                        addView(it, child, compositor)
                    }
                }
            it
        }
    }
}